//
//  Test3ViewController.swift
//  JKRegex
//
//  Created by 王冲 on 2018/2/4.
//  Copyright © 2018年 希爱欧科技有限公司. All rights reserved.
//

import UIKit

class Test3ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.white
        
        view.addSubview(label)
        
        /**
            1、pattern: 搜索的规则
            2、str: 传进去字符串
            3、replaceString: 要被替换的字符串
            4、font:字体的大小
         */
        let str: String = "This offer is not available offer cutomers living in <B>AK</b> and <B>HI</B>."
        label.attributedText = textRegexReplace(pattern: "offer", str: str, replaceString: "dog", font: 22)
    }

    // 2.替换内容
    func textRegexReplace(pattern: String,str: String,replaceString: String, font: CGFloat) -> NSMutableAttributedString{
        
        do {
            // 1.1.定义规则
            //let pattern = pattern
            // 1.2.创建正则表达式对象
            let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)
            // 1.3.开始匹配
            let res = regex.stringByReplacingMatches(in: str, options: NSRegularExpression.MatchingOptions.init(rawValue: 0), range: NSMakeRange(0, str.count), withTemplate:replaceString)
            
            // 1.4.给换过的内容加颜色
            return textRegex(pattern: replaceString, str: res, font: font)
            
        } catch {
            
            print(error)
        }
        return NSMutableAttributedString()
    }
    
    
    // 1.匹配纯文本
    func textRegex(pattern: String,str: String,font: CGFloat) -> NSMutableAttributedString{
        
        //富文本设置
        let attributeString = NSMutableAttributedString(string:str)
        do {
            // 1.1.定义规则
            //let pattern = pattern
            // 1.2.创建正则表达式对象
            let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)
            // 1.3.开始匹配
            let res = regex.matches(in: str, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, str.count))
            
            for checkingRes in res
            {
                print("range\(checkingRes.range)")
                
                // substring 截取符合规定规则的qq号
                print((str as NSString).substring(with: checkingRes.range))
                //从文本checkingRes.range个字符字体HelveticaNeue-Bold
                attributeString.addAttribute(NSAttributedStringKey.font, value: UIFont(name: "HelveticaNeue-Bold", size: 22)!,range: checkingRes.range)
                //设置字体颜色
                attributeString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.blue,range: checkingRes.range)
                //设置文字背景颜色
                attributeString.addAttribute(NSAttributedStringKey.backgroundColor, value: UIColor.green,range: checkingRes.range)
            }
            
            return attributeString
            
        } catch {
            
            print(error)
        }
        return attributeString
    }
    
    lazy var label: UILabel = {
        
        let label1: UILabel = UILabel(frame: CGRect(x: 16, y: 0, width: UIScreen.main.bounds.size.width-32, height: UIScreen.main.bounds.size.height))
        label1.font = UIFont.systemFont(ofSize: 22)
        label1.numberOfLines = 0
        label1.backgroundColor = UIColor.white
        return label1
    }()
}



